package y23.Sep;
//罗马字符转整数
public class Test6 {
    public static void main(String[] args) {
        String s = "I";
        System.out.println(romanToInt(s));
        s = "II";
        System.out.println(romanToInt(s));
        s = "III";
        System.out.println(romanToInt(s));
        s = "IV";
        System.out.println(romanToInt(s));
        s = "V";
        System.out.println(romanToInt(s));
        s = "VI";
        System.out.println(romanToInt(s));
        s = "VII";
        System.out.println(romanToInt(s));
        s = "III";
        System.out.println(romanToInt(s));
        s = "IV";
        System.out.println(romanToInt(s));
        s = "IX";
        System.out.println(romanToInt(s));
        s = "LVIII";
        System.out.println(romanToInt(s));
        s = "MCMXCIV";
        System.out.println(romanToInt(s));
    }

    public static int romanToInt(String s) {
        int res = 0;
        for (int i = 0; i < s.length(); i++) {
            int x = getValue(s.charAt(i));
            int y = 0;
            if(i+1 < s.length()){
                y = getValue(s.charAt(i+1));
            }
            if (x < y) {
                res -= x;
            } else {
                res += x;
            }
        }
        return res;
    }

    public static int getValue(char c) {
        switch (c) {
            case 'I':
                return 1;
            case 'V':
                return 5;
            case 'X':
                return 10;
            case 'L':
                return 50;
            case 'C':
                return 100;
            case 'D':
                return 500;
            case 'M':
                return 1000;
        }
        return 0;
    }
}
